package com.example.controller;

import com.example.bean.crm.zyb;
import com.example.service.crmService.crmService;
import com.example.service.k3Service.k3Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * 职员模块
 * Created by tony on 2017/12/15.
 */
@Component
@RestController
public class zyControl {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //定义k3Service
    @Autowired
    private k3Service k3Service;
    //定义mesService
    @Autowired
    private crmService crmService;

    /**
     * 职员初始化
     */
    @RequestMapping("/initializationZy")
    public void initializationZy() {
        logger.info("职员初始化。开始……");
        try {
            String model = "ALL";
            //取得全部职员信息
            ArrayList<zyb> zylist = k3Service.selectAllZy(model);
            //清空职员中间表
            crmService.deleteAllZy();
            if(zylist.size()>0){
                //中间表中插入全部职员信息
                crmService.initializationZy(zylist);
            }

        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        logger.info("职员初始化。结束。");

    }

    /**
     * 职员更新
     */
    @RequestMapping("/insertZy")
    @Scheduled(cron = "0 0 1 * * ?") //职员更新每天凌晨1点执行一次
    public void insertZyScheduled() {
        logger.info("职员更新每天凌晨1点执行一次。开始……");
        try {
            String model = "NEW";
            //取得更新的职员信息
            ArrayList<zyb> zylist = k3Service.selectAllZy(model);
            //判断更新和新建职员
            for (zyb zyb : zylist) {
                int zyFlg = zyb.getCRMclyf();
                //更新
                if (zyFlg == 1) {
                    //更新
                    crmService.updateZy(zyb);
                } else if (zyFlg == 0) {
                    //新增
                    crmService.insertZy(zyb);
                    k3Service.updateZy(zyb.getERPid());
                }
            }

        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        logger.info("职员更新每天凌晨1点执行一次。结束。");

    }

}
